//
//  CYHomeRecommendCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/4.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYHomeRecommendCell.h"



@implementation CYHomeRecommendCellObject

@end



@interface CYHomeRecommendCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *tagLabel;
@property(nonatomic, strong)CAShapeLayer *tagBorder;
@property(nonatomic, strong)UILabel *additionRateLabel;
@property(nonatomic, strong)UIImageView *additionRateBackgroundImageView;
@property(nonatomic, strong)CAShapeLayer *additionRateBorder;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UILabel *rateLabel;
@property(nonatomic, strong)UILabel *rateDescLabel;
@property(nonatomic, strong)UILabel *dueLabel;
@property(nonatomic, strong)UILabel *dueDescLabel;
@property(nonatomic, strong)UIView *line1;
@property(nonatomic, strong)UIView *line2;
@property(nonatomic, strong)UILabel *balanceDescLabel;
@property(nonatomic, strong)UILabel *balanceLabel;
@property(nonatomic, strong)UILabel *minDescLabel;
@property(nonatomic, strong)UILabel *minLabel;
@property(nonatomic, strong)UIButton *investButton;
@property(nonatomic, strong)CALayer *investShadowLayer;

@property(nonatomic, strong)CYHomeRecommendCellObject *object;

@end


@implementation CYHomeRecommendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = HEXCOLOR(@"FFFFFF");
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 5;
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        [self.contentView addSubview:self.titleLabel];
        
        self.tagLabel = [[UILabel alloc] init];
        self.tagLabel.textColor = APP_DEFAULT_COLOR_RED;
        self.tagLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
        self.tagLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.tagLabel];
        
        self.tagBorder = [[CAShapeLayer alloc] init];
        self.tagBorder.strokeColor = APP_DEFAULT_COLOR_RED.CGColor;
        self.tagBorder.lineWidth = 1 / SCREEN_SCALE;
        self.tagBorder.fillColor = [UIColor clearColor].CGColor;
        [self.tagLabel.layer addSublayer:self.tagBorder];
        
        self.additionRateBackgroundImageView = [[UIImageView alloc] init];
        self.additionRateBackgroundImageView.image = [UIImage gradientImageFromColors:@[HEXCOLOR(@"F7694E"), APP_DEFAULT_COLOR_RED] ByGradientType:UIImageGradientTypeLeftToRight size:CGSizeMake(100, 15 + 2 / SCREEN_SCALE)];
        [self.contentView addSubview:self.additionRateBackgroundImageView];
        
        self.additionRateBorder = [[CAShapeLayer alloc] init];
        self.additionRateBackgroundImageView.layer.mask = self.additionRateBorder;
        
        self.additionRateLabel = [[UILabel alloc] init];
        self.additionRateLabel.textColor = HEXCOLOR(@"ffffff");
        self.additionRateLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.additionRateLabel];
        
        self.line = [UIView cy_lineWithColor:HEXCOLOR(@"EDEDED")];
        [self.contentView addSubview:self.line];
        
        self.rateDescLabel = [[UILabel alloc] init];
        self.rateDescLabel.text = @"预期年化收益";
        self.rateDescLabel.textColor = HEXCOLOR(@"999999");
        self.rateDescLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [self.rateDescLabel sizeToFit];
        [self.contentView addSubview:self.rateDescLabel];

        self.rateLabel = [[UILabel alloc] init];
        self.rateLabel.textColor = APP_DEFAULT_COLOR_RED;
        self.rateLabel.font = [UIFont systemFontOfSize:28 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.rateLabel];
        
        self.line1 = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.line1];
        
        self.line2 = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.line2];
        
        self.dueLabel = [[UILabel alloc] init];
        self.dueLabel.textColor = HEXCOLOR(@"444444");
        self.dueLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.dueLabel];
        
        self.dueDescLabel = [[UILabel alloc] init];
        self.dueDescLabel.textColor = HEXCOLOR(@"999999");
        self.dueDescLabel.text = @"期限";
        self.dueDescLabel.font = [UIFont systemFontOfSize:12];
        [self.dueDescLabel sizeToFit];
        [self.contentView addSubview:self.dueDescLabel];
        
        self.balanceDescLabel = [[UILabel alloc] init];
        self.balanceDescLabel.textColor = HEXCOLOR(@"999999");
        self.balanceDescLabel.text = @"可投金额";
        self.balanceDescLabel.font = [UIFont systemFontOfSize:12];
        [self.balanceDescLabel sizeToFit];
        [self.contentView addSubview:self.balanceDescLabel];

        self.minDescLabel = [[UILabel alloc] init];
        self.minDescLabel.textColor = HEXCOLOR(@"999999");
        self.minDescLabel.text = @"起投金额";
        self.minDescLabel.font = [UIFont systemFontOfSize:12];
        [self.minDescLabel sizeToFit];
        [self.contentView addSubview:self.minDescLabel];
        
        self.balanceLabel = [[UILabel alloc] init];
        self.balanceLabel.textColor = HEXCOLOR(@"444444");
        self.balanceLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.balanceLabel];

        self.minLabel = [[UILabel alloc] init];
        self.minLabel.textColor = HEXCOLOR(@"444444");
        self.minLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.minLabel];
        
        self.investShadowLayer = [CALayer layer];
        self.investShadowLayer.cornerRadius = 20;
        self.investShadowLayer.backgroundColor = APP_DEFAULT_COLOR_RED.CGColor;
        self.investShadowLayer.shadowColor = HEXCOLORA(@"E94545", 0.3).CGColor;
        self.investShadowLayer.shadowOpacity = 1;
        self.investShadowLayer.shadowOffset = CGSizeMake(0, 2);
        [self.contentView.layer addSublayer:self.investShadowLayer];

        self.investButton = [[UIButton alloc] init];
        self.investButton.layer.masksToBounds = YES;
        self.investButton.layer.cornerRadius = 20;
        [self.investButton setBackgroundImage:[UIImage imageWithColor:APP_DEFAULT_COLOR_RED size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
        [self.investButton setTitle:@"立即投资" forState:UIControlStateNormal];
        [self.investButton setTitleColor:HEXCOLOR(@"ffffff") forState:UIControlStateNormal];
        self.investButton.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
        [self.investButton addTarget:self action:@selector(onInvestButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.investButton];
}
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 240;
}

- (BOOL)shouldUpdateCellWithObject:(CYHomeRecommendCellObject *)object
{
    self.object = object;
    if(object.model)
    {
        self.titleLabel.text = object.model.title;
        NSString *tag = SAFE_STRING(object.model.tag);
        if(tag.length)
        {
            self.tagLabel.hidden = NO;
            self.tagLabel.text = tag;
        }
        else
        {
            self.tagLabel.hidden = YES;
        }
        
        double additionRate = SAFE_NUMBER(object.model.additionRate).doubleValue;
        if(additionRate > 0)
        {
            self.additionRateLabel.hidden = NO;
            self.additionRateBackgroundImageView.hidden = NO;
            self.additionRateLabel.text = [NSString stringWithFormat:@"已贴息%.2f%%", additionRate];
        }
        else
        {
            self.additionRateLabel.hidden = YES;
            self.additionRateBackgroundImageView.hidden = YES;
        }
        
        
        self.rateLabel.text = [NSString stringWithFormat:@"%.2f%%", SAFE_NUMBER(object.model.rate).doubleValue + SAFE_NUMBER(object.model.additionRate).doubleValue];
        self.dueLabel.text = [NSString stringWithFormat:@"%@天", SAFE_NUMBER(object.model.due)];
        self.balanceLabel.text = [NSString stringWithFormat:@"%@元", [SAFE_NUMBER(object.model.balance).amountString componentsSeparatedByString:@"."].firstObject];
        self.minLabel.text = [NSString stringWithFormat:@"%@元", [SAFE_NUMBER(object.model.start).amountString componentsSeparatedByString:@"."].firstObject];
        self.investButton.hidden = NO;
        self.investShadowLayer.hidden = NO;
    }
    else
    {
        self.rateLabel.attributedText = nil;
        self.dueLabel.attributedText = nil;
        self.titleLabel.text = nil;
        self.balanceLabel.text = nil;
        self.minLabel.text = nil;
        self.investButton.hidden = YES;
        self.investShadowLayer.hidden = YES;
        self.tagLabel.hidden = YES;
    }

    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.contentView.width = self.width - 20;
    self.contentView.height = self.height;
    self.contentView.top = 0;
    self.contentView.centerX = self.width / 2;
    
    {
        [self.titleLabel sizeToFit];
        self.titleLabel.centerY = 23;
        
        [self.tagLabel sizeToFit];
        
        [self.additionRateLabel sizeToFit];
        self.additionRateBackgroundImageView.height = 15 + 2 / SCREEN_SCALE;
        self.additionRateBackgroundImageView.width = self.additionRateLabel.width + 12;

        self.tagLabel.centerY = 23;
        self.tagBorder.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.tagLabel.width + (self.additionRateLabel.hidden ? 12 : 16), 15) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)].CGPath;
        self.tagBorder.frame = CGRectMake(-6, (self.tagLabel.height - 15) / 2, self.tagLabel.width + (self.additionRateLabel.hidden ? 12 : 16), 15);
        
        self.additionRateBorder.path = [UIBezierPath bezierPathWithRoundedRect:self.additionRateBackgroundImageView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)].CGPath;
        self.additionRateBorder.frame = self.additionRateBackgroundImageView.bounds;
        
        {
            CGFloat width = self.titleLabel.width;
            if(self.tagLabel.hidden == NO)
            {
                width += 11;
                width += self.tagLabel.width;
                
                if(self.additionRateBackgroundImageView.hidden == NO)
                {
                    width += 5;
                    width += self.additionRateBackgroundImageView.width;
                }
            }
            else if(self.additionRateBackgroundImageView.hidden == NO)
            {
                width += 5;
                width += self.additionRateBackgroundImageView.width;
            }
            
            if(width > self.contentView.width - 20)
            {
                self.titleLabel.width -= (width - self.contentView.width + 20);
                width = self.contentView.width - 20;
            }
            
            self.titleLabel.left = (self.contentView.width - width) / 2;
            if(self.tagLabel.hidden == NO)
            {
                self.tagLabel.centerY = self.titleLabel.centerY;
                self.tagLabel.left = self.titleLabel.right + 11;
                
                if(self.additionRateBackgroundImageView.hidden == NO)
                {
                    self.additionRateBackgroundImageView.centerY = self.titleLabel.centerY;
                    self.additionRateBackgroundImageView.left = self.tagLabel.right + 5;
                    self.additionRateLabel.center = self.additionRateBackgroundImageView.center;
                }
            }
            else if(self.additionRateBackgroundImageView.hidden == NO)
            {
                self.additionRateBackgroundImageView.centerY = self.titleLabel.centerY;
                self.additionRateBackgroundImageView.left = self.titleLabel.right + 5;
                self.additionRateLabel.center = self.additionRateBackgroundImageView.center;
            }
        }
    }
    
    self.line.width = self.contentView.width;
    self.line.height = 1 / SCREEN_SCALE;
    self.line.left = 0;
    self.line.bottom = 45;
    
    self.rateDescLabel.centerX = self.contentView.width / 2;
    self.rateDescLabel.centerY = 71;

    [self.rateLabel sizeToFit];
    self.rateLabel.centerY = 99;
    self.rateLabel.centerX = self.contentView.width / 2;
    
    self.line1.width = self.line2.width = 1 / SCREEN_SCALE;
    self.line1.height = self.line2.height = 20;
    self.line1.centerY = self.line2.centerY = 146.5;
    self.line1.centerX = self.contentView.width / 3;
    self.line2.centerX = self.contentView.width * 2 / 3;
    
    [self.dueLabel sizeToFit];
    [self.balanceLabel sizeToFit];
    [self.minLabel sizeToFit];
    self.dueLabel.centerY = self.balanceLabel.centerY = self.minLabel.centerY = 154;
    self.dueLabel.centerX = self.contentView.width / 6;
    self.balanceLabel.centerX = self.contentView.width * 3 / 6;
    self.minLabel.centerX = self.contentView.width * 5 / 6;
    
    self.dueDescLabel.centerY = self.balanceDescLabel.centerY = self.minDescLabel.centerY = 137;
    self.dueDescLabel.centerX = self.contentView.width / 6;
    self.balanceDescLabel.centerX = self.contentView.width * 3 / 6;
    self.minDescLabel.centerX = self.contentView.width * 5 / 6;
    
    self.investButton.width = self.contentView.width - 37 * 2;
    self.investButton.height = 40;
    self.investButton.centerX = self.contentView.width / 2;
    self.investButton.top = 180;
    
    self.investShadowLayer.frame = self.investButton.frame;
}

#pragma mark - Control Events
- (void)onInvestButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0013];
    if(self.object.investAction)
    {
        self.object.investAction();
    }
}

@end
